package com.org.khtn.command.insert;

import java.util.HashMap;
import java.util.Map;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.org.khtn.command.MyParse;
import com.org.khtn.command.QueryPattern;
import com.org.khtn.stringcommand.Format;

public class ParseInsert extends MyParse{
	
	final String className= getClass().getSimpleName()+":";

	public ParseInsert(String Query) {
		query = Query;

	}
	@Override
	public boolean eval() {
		final String methodName = getClass().getSimpleName()+"():";
		//System.out.println(className+methodName+"pattern:"+QueryPattern.INSERT_PATTERN);
		Pattern a = Pattern.compile(QueryPattern.INSERT_PATTERN, Pattern.CASE_INSENSITIVE);
		Matcher b = a.matcher(query);

		return b.matches();

	}
	@Override
	public String[] parse() {
		if (!eval())
			return null;
		Format f =new Format(query);
		query = f.XLC();
		int idx = query.indexOf('(');
		String tam = query.substring(0,idx-1);
		String[] temp = tam.split("\\s+");
		String tam1 = query.substring(idx,query.length());
		String[] temp2 = tam1.split("\\s+values\\s+");
		String[] result  = new String[] {temp[2],temp2[0],temp2[1]};
		for (int i=0; i<3;i++)
		{
			while ((result[i].charAt(0) == '(' && result[i].charAt(result[i]
					.length() - 1) == ')')) {
				result[i] = result[i].substring(1, result[i].length() - 1);
			}
			System.out.println(result[i]);

		}
		return result;
	}

//	public static void main(String[] args) {
//		ParseInsert p = new ParseInsert(
//				"insert into sach (masach,name) values ('18','thao khung')");
//		System.out.println(p.eval());
//		p.parse();
//	}
}
